﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HookahProject.Models
{
    public class DataManager
    {

        private HookahEntities2 _wn;

        public DataManager()
        {
            _wn = new HookahEntities2();
        }

        public IQueryable<Tobacco> GetTobaccos()
        {
            return _wn.Tobacco;
        }

        public Tobacco GetTobacco(int id)
        {
            return _wn.Tobacco.SingleOrDefault(it => it.Id == id);
        }

        public void AddTobacco(string Brand, string Flavor, float Price, float Rating, string Comments, string IsExists, string UserId)
        {
            var newTobacco = new Tobacco { Brand = Brand, Flavor = Flavor, Price = Price, Rating = Rating, Comments = Comments, IsExists = IsExists, UserId = UserId };
            _wn.Tobacco.AddObject(newTobacco);
            _wn.SaveChanges();
        }

        public void EditTobacco(Tobacco obj)
        {
            Tobacco old = GetTobacco(obj.Id);
            old.Brand = obj.Brand;
            old.Flavor = obj.Flavor;
            old.Price = obj.Price;
            old.Rating = obj.Rating;
            old.Comments = obj.Comments;
            old.IsExists = obj.IsExists;
            _wn.SaveChanges();
                
        }

        public void DeleteTobacco(Tobacco obj)
        {
            Tobacco del = GetTobacco(obj.Id);
            _wn.DeleteObject(del);
            _wn.SaveChanges();
        }

        public void AddMixture(float MixtureRating, string Comments, string UserId, List<int>Tobacco)
        {
            var newMixture = new Mixture { MixtureRating = MixtureRating, Comments = Comments, UserId = UserId };
            _wn.Mixture.AddObject(newMixture);
            foreach(int TobaccoId in Tobacco)
            {
                var newTobaccoMixture = new TobaccoMixture {TobaccoId = TobaccoId, MixtureId = newMixture.Id}; 
                _wn.TobaccoMixture.AddObject(newTobaccoMixture);
            }
           
            _wn.SaveChanges();
        }

       
        
    }
}